//
//  AddSkillVC.m
//  MyProject
//
//  Created by jiaming yan on 2017/9/13.
//  Copyright © 2017年 yanjiaming. All rights reserved.
//

#import "AddSkillVC.h"
#import "GetResumeByUserIdSkillList.h"
#import "NSString+Expand.h"

@interface AddSkillVC ()
@property (weak, nonatomic) IBOutlet UIButton *typeButton1;
@property (weak, nonatomic) IBOutlet UIButton *typeButton2;
@property (weak, nonatomic) IBOutlet UIButton *typeButton3;
@property (weak, nonatomic) IBOutlet UITextField *tfSkillName;
@property (assign, nonatomic) NSInteger skillProficiency;
@property (weak, nonatomic) IBOutlet UIView *viewBtnDeleteBg;
@end

@implementation AddSkillVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"编辑技能";
    self.typeButton1.layer.borderColor = UIColorFromRGB(0X43C4D1).CGColor;
    self.typeButton2.layer.borderColor = UIColorFromRGB(0X43C4D1).CGColor;
    self.typeButton3.layer.borderColor = UIColorFromRGB(0X43C4D1).CGColor;
    self.skillProficiency = -1;
    self.view.backgroundColor = kWhiteColor;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x13bacd) forState:UIControlStateNormal];
    button.titleLabel.font = k15Font;
    button.frame = CGRectMake(0, 0, 40, 40);
    [button addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,barItem];
    if (IOS11Later) {
        button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
    
    if (!self.model) {
        //如果是增加,需要隐藏最底端按钮
        self.viewBtnDeleteBg.hidden = YES;
    }else{
        //否则,需要把原始值付进来
        [self setVaules];
    }
}

- (void)setVaules{
    self.tfSkillName.text = self.model.skillName;
    NSArray *array = @[@"一般",@"熟练",@"精通"];
    NSInteger index = 0;
    for (NSInteger i = 0; i<array.count; i++) {
        if ([self.model.proficiency isEqualToString:array[i]]) {
            index = i;
        }
    }
    self.skillProficiency = index;
    switch (index) {
        case 0:
        {
            self.typeButton1.backgroundColor = UIColorFromRGB(0x1AA9BA);
            [self.typeButton1 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        }
            break;
        case 1:
        {
            self.typeButton2.backgroundColor = UIColorFromRGB(0x1AA9BA);
            [self.typeButton2 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            self.typeButton3.backgroundColor = UIColorFromRGB(0x1AA9BA);
            [self.typeButton3 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}

-(void)saveButtonClick{
    
    NSString *strAlert = @"";
    
    if (self.tfSkillName.text.length == 0) {
        strAlert = @"请输入技能名称";
    }else if (self.skillProficiency == -1){
        strAlert = @"请选择熟练度";
    }
    
    if (strAlert.length != 0) {
        [CommonUtils showToast:strAlert];
        return;
    }
    NSArray *array = @[@"一般",@"熟练",@"精通"];
    
    NSDictionary *dicModelNow = [[NSDictionary alloc]initWithObjectsAndKeys:self.tfSkillName.text,@"skillName",array[self.skillProficiency],@"proficiency",nil];

    [self showProgressWithView:self.view animated:YES];
    if (self.model) {
        dicModelNow = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:self.model.candidateSkillsId],@"candidateSkillsId",self.tfSkillName.text,@"skillName",array[self.skillProficiency],@"proficiency",nil];
        
    }
    [YJMSendRequest postDataWithParameters:dicModelNow url:@"resume/updateSkill" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    

}


- (IBAction)btnDeleteOnClick:(id)sender {
    NSDictionary *dicModel = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:self.model.candidateSkillsId],@"candidateSkillsId",nil];
    [self showProgressWithView:self.view animated:YES];
    
    [YJMSendRequest postDataWithParameters:dicModel url:@"resume/deleteCandidateSkills" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];

}


- (IBAction)typeButtonClick:(UIButton *)sender {
    if (sender == self.typeButton1) {
        self.typeButton1.backgroundColor = UIColorFromRGB(0x1AA9BA);
        [self.typeButton1 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        
        self.typeButton2.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton2 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        
        self.typeButton3.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton3 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        self.skillProficiency = 0;
    }else if (sender == self.typeButton2) {
        self.typeButton2.backgroundColor = UIColorFromRGB(0x1AA9BA);
        [self.typeButton2 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        
        self.typeButton1.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton1 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        
        self.typeButton3.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton3 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        self.skillProficiency = 1;

    }else if (sender == self.typeButton3) {
        self.typeButton3.backgroundColor = UIColorFromRGB(0x1AA9BA);
        [self.typeButton3 setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        
        self.typeButton2.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton2 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        
        self.typeButton1.backgroundColor = UIColorFromRGB(0xffffff);
        [self.typeButton1 setTitleColor:UIColorFromRGB(0x6C7374) forState:UIControlStateNormal];
        self.skillProficiency = 2;

    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
